package com.shanzmoo.admin.web.store;

import com.shanzmoo.admin.domain.dto.StoreMemberSaveDTO;
import com.shanzmoo.admin.domain.dto.StoreMemberUpdateDTO;
import com.shanzmoo.admin.manager.store.StoreMemberManager;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.store.entity.bo.StoreMemberSelectBO;
import com.shanzmoo.db.store.entity.query.StoreMemberQuery;
import com.shanzmoo.db.store.entity.vo.StoreMemberVO;
import com.shanzmoo.db.store.service.IStoreMemberService;
import com.shanzmoo.db.store.entity.StoreMemberEntity;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.util.List;

/**
 * 仓储客户 控制层
 *
 * @author Gzhao 2021-03-08
 */
@Api(tags = "仓储-客户")
@RestController
@RequestMapping("/store/member")
public class StoreMemberController {

    @Autowired
    private IStoreMemberService storeMemberService;
    @Autowired
    private StoreMemberManager storeMemberManager;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreMemberVO>> pageList(@Valid @RequestBody StoreMemberQuery query) {
        PageRespBase<StoreMemberVO> pageList = storeMemberManager.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody StoreMemberSaveDTO saveDto) {
        storeMemberManager.addMember(saveDto.convertEntity(), CurrentUserCache.getUserId());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "id") Integer id) {
        StoreMemberEntity entity = new StoreMemberEntity();
        entity.setId(id);
        storeMemberService.updateById(entity);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody StoreMemberUpdateDTO updateDto) {
        storeMemberService.updateById(updateDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "重置密码")
    @GetMapping("/rest-password")
    public SHttpResponse<Boolean> restPassword(@RequestParam(value = "id") Integer id) {
        storeMemberManager.restPassword(id, CurrentUserCache.getUserId());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "启用（禁用）")
    @GetMapping("/update-status")
    public SHttpResponse<Boolean> updateStatus(@RequestParam(value = "id") Integer id) {
        storeMemberManager.updateStatus(id, CurrentUserCache.getUserId());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "选择列表")
    @GetMapping("/selectList")
    public SHttpResponse<List<StoreMemberSelectBO>> selectList() {
        List<StoreMemberSelectBO> list = storeMemberService.getSelectList();
        return SHttpResponse.ok(list);
    }
 }
